#encoding=utf8

import aaf
from aaf.component import Sequence
from aaf.component import Timecode
from aaf.mob import CompositionMob
from aaf.storage import ContentStorage

from optparse import OptionParser


def walk_properties(space, iter_item):
    parser = OptionParser()
    parser.add_option('--show_dictionary', action='store_true', default=False, dest='show_dict',
                      help="show dictionary properties")
    parser.add_option('--show_stream', action='store_true', default=False, dest='show_stream',
                      help="show binary stream data")
    (options, args) = parser.parse_args()

    for item in iter_item:
        value = item
        if isinstance(item, aaf.property.PropertyItem):
            value = item.value
        name = ""

        if hasattr(item, 'name'):
            name = item.name or ""

        print(space, name, value)

        if isinstance(value, aaf.dictionary.Dictionary) and not options.show_dict:
            continue
        # don't dump out stream data, its ugly
        if isinstance(value, aaf.iterator.TypeDefStreamDataIter) and not options.show_stream:
            continue

        s = space + '   '
        if isinstance(value, aaf.base.AAFObject):
            walk_properties(s, value.properties())
        if isinstance(value, aaf.iterator.BaseIterator):
            walk_properties(s, value)
        pass


output_aaf = 'resources/onlyone.aaf'

aaf_file = aaf.open(output_aaf, 'r')
print "[aaf_file]:" + str(aaf_file)
print

class_iid = aaf_file.class_iid
header = aaf_file.header
storage = aaf_file.storage
dictionary = aaf_file.dictionary

print "[aaf_file.class_iid]:" + str(class_iid)
print "[aaf_file.header]:" + str(header)
print "[aaf_file.storage]:" + str(storage)
print "[aaf_file.dictionary]:" + str(dictionary)
print

content = header.get_value("Content")
mobs = content.get_value("Mobs")

aaf_mobs = storage.mobs()
for aaf_mob in aaf_mobs:
    print aaf_mob
    if "MasterMob" in str(type(aaf_mob)):
        masterMob = aaf_mob

    if "CompositionMob" in str(type(aaf_mob)):

        #CompositionMob下面有多个Slot，添加TimelineMobSlot方式
        # timecode = Timecode(aaf_file,100,0,24)
        # 添加TimelineMobSlot，当前理解为Track
        # segment = Sequence(aaf_file,media_kind="picture")
        # aaf_mob.append_new_timeline_slot(24,segment,slot_name="Track")
        # 未知效果
        # aaf_mob.create_clip()
        # 给Mob的属性key值为Attributes的对象，里面添加key,value
        # aaf_mob.append_attribute('testValue','Composition TimeLine')
        # 获取Mob的数据
        # name = aaf_mob.get_value("Name")
        print aaf_mob
        # storage.remove_mob(aaf_mob)
        # storage.add_mob(aaf_mob)

        for slot in aaf_mob.slots():
            print "    " + str(slot) + " " + str(type(slot))

            if "TimelineMobSlot" in str(type(slot)):
                print slot.mark_in
                print slot.mark_out
                segment = slot.segment
                print "    " + "    " + str(segment) + " " + str(type(slot))

                if "Sequence" in str(type(segment)):
                    sequence = segment
                    # 错误的用法，但是不知道如何使用
                    # positions = sequence.positions()
                    # index, edit_start_time, component = sequence.positions()
                    # print "[sequence.index]:" + str(index)
                    # print "[sequence.edit_start_time]:" + str(edit_start_time)
                    # print "[sequence.component]:" + str(component)
                    for component in sequence.components():
                        print "    " + "    " + "    " + str(component) + " " + str(type(component))
                        if "OperationGroup" in str(type(component)):
                            # inputsegments = component.input_segments()
                            # print "    " + "    " + "    " + "    " + str(inputsegments) + " " + str(type(inputsegments))
                            for input_segment in component.input_segments():
                                print "    " + "    " + "    " + "    " + str(input_segment) + " " + str(type(input_segment))
                                if "SourceClip" in str(type(input_segment)):
                                    sourceclip = input_segment
                                    print "    " + "    " + "    " + "    " + "[sourceclip.start_time]:" + str(sourceclip.start_time)
                                    print "    " + "    " + "    " + "    " + "[sourceclip.mob_id]:" + str(sourceclip.mob_id)
                                    print "    " + "    " + "    " + "    " + "[sourceclip.source_ref]:" + str(sourceclip.source_ref)
                                    # start_time = sourceclip.start_time
                                    # source_ref = sourceclip.source_ref
                                    # print start_time
                                    # # print sourceclip.mob_id
                                    # print source_ref

# This success to create CompositionMob
# new_timeline = CompositionMob(aaf_file,"AAF Test TimeLine")
# storage.add_mob(new_timeline)
aaf_file.save("resources/change_onlyone_timeline_name.aaf")
print "[aaf_file]:" + str(aaf_file)

# dump result file
print "Dump result file Content:"
walk_properties("", aaf_file.header.properties())

aaf_file.close()